import { GetAirPort } from "@/api/public";
import hooks from "@/hooks";
import { getToken } from "@/utils/auth";
import { cancel } from "@/utils/common/common";
import { dictEnum } from "@/utils/common/enums/dictEnum";
import { getCountryList } from "@/utils/common/generalUtils";
import locale from "@/utils/i18n";
import { Session } from "@/utils/storage";
const saveConutry = ref([]);
const redisExpatPointOriginDatas: any = ref([]);//作为中间变量保存
export let nationalityOptions = ref([]);
export let expatPointOriginDatas: any = ref([])//机场信息
export let fetching = ref(false);
const objs: any = reactive({
    type: null
})
let i = 0
//缓存多个对象的值
const redisObjs: any = reactive({})
//获取国家信息
watchEffect(() => {
})
export class SearchSelectHelper {
    // [type: string]: any;
    constructor(obj: any) {
        getCountryList().then((r: any) => {
            saveConutry.value = r ?? []
            nationalityOptions.value = r ?? []
        });
        objs.type = obj ? obj.type : undefined;
    }

    search = debounce(async (value) => {
        const { isCn, isEn } = hooks.useLanguage()
        fetching.value = true
        //当前是国家搜索
        if (objs.type === dictEnum.country) {
            nationalityOptions.value = saveConutry.value
            if (value.length < 1) { fetching.value = false; return; }
            let newArr: any = [];
            nationalityOptions.value?.map((o: any) => {
                (isCn.value ? o.nameZh.includes(value) : isEn.value ? o.nameEn.includes(value) : o.nameAr.includes(value)) && newArr.push(o);
            })
            nationalityOptions.value = newArr;
            fetching.value = false
        }
        //机场搜索
        if (objs.type === dictEnum.expatPointOrigin) {
            // expatPointOriginDatas.value = null
            console.log('==value======value========>', value);

            expatPointOriginDatas.value = []
            if (value.length < 1) return
            if (value.length > 0) {
                const { data }: any = await GetAirPort({ keyword: value }).catch(error => {
                    console.error(error);
                }).finally(() => { (fetching.value = false) })
                expatPointOriginDatas.value = data?.map((h: any) => {
                    return { ...h, expatPointOrigin: `${h.name}` }
                })
            }
        }
    }, 500)
    /**
     * 改变的值
     * @param v 点击获取到的国家id
     * @returns 
     */
    change(v: any) {
        switch (objs.type) {
            case dictEnum.country:
                return v;
            case dictEnum.expatPointOrigin:
                return v;
        }
    }
}
